package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.CauseBean;
import com.sanyang.pda.bean.GoodsDetailBean;
import com.sanyang.pda.bean.HaveTestedBean;
import com.sanyang.pda.bean.QualityDetailBean;
import com.sanyang.pda.bean.ReasonListBean;
import com.sanyang.pda.contract.QualityDetailContract;
import com.sanyang.pda.event.SamplingEvent;
import com.sanyang.pda.net.NetUtils;
import com.sanyang.pda.utils.UserUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class QualityDetailPresenter extends BasePresenter<QualityDetailContract.View> implements QualityDetailContract.Presenter {


    @Override
    public void palletizingInfo(String samplingNo) {
        NetUtils.geApi().palletizingInfo(samplingNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<QualityDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<QualityDetailBean> listBaseAppEntity) throws Exception {
                        getView().setData(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                        getView().finishActivity();
                    }
                });
    }

    @Override
    public void addReasonSave(CauseBean causeBean) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(causeBean));
        NetUtils.geApi().addReasonSave(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().delectaddSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void deleteOneDetail(String samplingResultNo) {
        NetUtils.geApi().deleteOneDetail(samplingResultNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<HaveTestedBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<HaveTestedBean>> listBaseAppEntity) throws Exception {
                        getView().delectaddSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void qualityInspectionCompleted(String samplingNo) {
        JSONObject jsonObject=new JSONObject();
        try {
            jsonObject.put("samplingNo",samplingNo);
            jsonObject.put("checkName", UserUtils.getInstance().getUserBean().getUserName());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), jsonObject.toString());
        NetUtils.geApi().qualityInspectionCompleted(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().showMessage(goodsDetailBeanBaseAppEntity.getMsg());
                        EventBus.getDefault().post(new SamplingEvent());
                        getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectType() {
        NetUtils.geApi().selectType("reason")
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<ReasonListBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<ReasonListBean>> byBarcodeBeanBaseAppEntity) throws Exception {
                        getView().setReasonList(byBarcodeBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
